﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Forms;
using StockTradTest.Data;
using Plugin.Toast;
using Plugin.Toast.Abstractions;
using StockTradTest.strategy;
using Xamarin.Essentials;

namespace StockTradTest
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        bool systemRun = false;
        bool isAlarmOpen = false;

        public static string code = "601166";
        public static GetStockHistory getStockHistory;
        public static RealTimeInfo realTimeStock;

        private static INotificationManager notificationManager;
        public MainPage()
        {
            InitializeComponent();
            getStockHistory = new GetStockHistory();
            realTimeStock = new RealTimeInfo();
            contentText.Text = "";
            //DependencyService应用程序从共享代码调用本机平台功能的服务定位器。
            notificationManager = DependencyService.Get<INotificationManager>();
            //通知管理器添加事件
            notificationManager.NotificationReceived += (sender, eventArgs) =>
            {
                //将接收到的信息转化为通知事件信息
                var evtData = (NotificationEventArgs)eventArgs;
                //显示
                ShowNotification(evtData.Title, evtData.Message);
            };
        }
        void OnStartButtonClicked(object sender, EventArgs e)
        {
            startButton.IsEnabled = false;
            runningShow.IsEnabled = true;
            runningShow.IsRunning = true;
            if (!systemRun)
            {
                systemRun = true;
                startWarningText.Text = "系统已启动";
                startButton.Text = "停止系统";
                CrossToastPopUp.Current.ShowToastSuccess("启动", ToastLength.Short);
                Run();
                
            }
            else
            {
                Stop();
                contentText.Text = "";
                startWarningText.Text = "系统已停止";
                startButton.Text = "启动系统";
            }
            startButton.IsEnabled = true;
            runningShow.IsRunning = false;
            runningShow.IsEnabled = false;
        }
        void OnAlarmButtonClicked(object sender, EventArgs e)
        {
            if (alarmWarning.IsToggled)
                isAlarmOpen = true;
            else
                isAlarmOpen = false;
        }
        void OnCloundButtonClicked(object sender, EventArgs e)
        {
            if (cloundButton.IsToggled)
                TestTradBase.isOnClound = true;
            else
                TestTradBase.isOnClound = false;
        }
        void Run()
        {
            realTimeStock = GetRealTimeInfo.GetStockRealTimeInfo(code);
            string temp = string.Format("股票{0}最新价格:{1},当前时间:{2}\r\n",
            code, realTimeStock.currentPrice.ToString(), DateTime.Now.ToLongTimeString().ToString());
            if(TestTradBase.isOnClound)
            {
                WxPusher.HttpGet(temp);
            }
            else
            {
                SendAlarm(temp);
            }
            //不修改账户信息，直接修改realtime实时
            TestTradBase.InitalMyAccountWithData(true);
            Device.StartTimer(TimeSpan.FromSeconds(1),  () =>
            {
                // do something every 1 seconds

                if (!systemRun) return false;

                ClearText();
                if(isAlarmOpen)
                {
                    _ = MyStrategy.TradeAberationRealRunAsync(code, 5);
                    contentText.Text = TestTradBase.record;
                }
                
                return systemRun;
            });
        }
        void Stop()
        {
            systemRun = false;
        }
        void ClearText()
        {
            string[] temp = contentText.Text.Split('\r');
            if (temp.Length > 30) contentText.Text = contentText.Text.Substring(temp[0].Length + 1);
        }
        void ShowNotification(string title, string message)
        {
            DisplayAlert(title, message, "确认");
            Vibration.Cancel();
        }

        public static void SendAlarm(string temp)
        {
            notificationManager.SendNotification("交易信息", temp);
            Vibration.Vibrate(1000);
        }
    }
}
